import time
from . import ConnPG
import psycopg2
from sanic.log import logger

class ClassTable:

    async def add_ancestor(self, class_name):#添加祖类名，本系统为一级分类

        #参数检查

        if not class_name:

            return {
                'status': -1,
                'errorCode': 106100,
                'errorMsg': '祖类名不能为空',
                'resultBody': None
            }

        sql = 'INSERT INTO class(class_name) VALUES (%s)'

        try:
            await ConnPG().execute(sql, (class_name,))
        except psycopg2.errors.UniqueViolation:

            return {

                'status': -1,
                'errorCode': 106101,
                'errorMsg': '当前类名已经存在',
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "祖类添加成功",
            }

    async def add_son(self, son_name, parent_id):#添加二级分类

        #参数检查
        if not(son_name and parent_id) or not parent_id.isdecimal():

            return {
                'status': -1,
                'errorCode': 106102,
                'errorMsg': '两个参数为必传参数，且parent_id应为整数',
                'resultBody': None
            }

        parent_id = int(parent_id)

        #查查parent_id合法性，第一要存在，本系统中应为祖节点即一级分类

        check_sql = 'select * from class where id=%s and parent_id is null'
        check_result = await ConnPG().fetchone(check_sql, (parent_id,))

        if not check_result:

            return {
                'status': -1,
                'errorCode': 106103,
                'errorMsg': 'parent_id应为一级分类',
                'resultBody': None
            }

        sql = 'INSERT INTO class(class_name, parent_id) VALUES (%s, %s)'

        try:
            await ConnPG().execute(sql, (son_name, parent_id))
        except psycopg2.errors.UniqueViolation as e:

            # print(e)
            logger.warning(str(e))

            return {
                'status': -1,
                'errorCode': 106101,
                'errorMsg': '当前类名已经存在',
                'resultBody': None,
            }

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "职务添加成功",
        }

    async def select_all_ancestor(self):#查看所有一级分类

        sql = 'select * from class where parent_id is null'

        result = await ConnPG().fetchall(sql)

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def select_son(self, now_class):#查看当前节点的儿子节点

        '''
        now_class：字符串
        如果now_class.isdecimal()为True则使用ID查看，否则使用类名查找id
        '''

        #参数检查
        if not now_class:

            return {
                'status': -1,
                'errorCode': 106104,
                'errorMsg': '接口有一个必传参数，请检查参数',
                'resultBody': None
            }

        if not now_class.isdecimal():

            sql = "select * from class where class_name=%s"
            class_info = await ConnPG().fetchone(sql, (now_class,))
            if class_info == None:
                return {
                    'status': -1,
                    'errorCode': 106105,
                    'errorMsg': '当前节点不存在',
                    'resultBody': None
                }
            class_id = class_info['id']
        else:
            class_id = int(now_class)
        
        result_sql = "select * from class where parent_id=%s"
        result = await ConnPG().fetchall(result_sql, (class_id,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def select_class(self, now_class):#查看当前节点的信息

        '''
        now_class：字符串
        如果now_class.isdecimal()为True则使用ID查看，否则使用类名查找id
        '''

        #参数检查
        if not now_class:

            return {
                'status': -1,
                'errorCode': 106106,
                'errorMsg': '接口有一个必传参数，请检查参数',
                'resultBody': None
            }

        if not now_class.isdecimal():

            sql = "select * from class where class_name=%s"
            
        else:
            sql = "select * from class where id=%s"
            
        result = await ConnPG().fetchone(sql, (now_class,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def del_class(self, class_id):#删除class

        #参数检查

        if not class_id or not class_id.isdecimal:

            return {
                'status': -1,
                'errorCode': 106106,
                'errorMsg': 'class_id不能为空且为整数类型',
                'resultBody': None
            }

        sql = 'DELETE FROM class WHERE id=%s'

        try:
            await ConnPG().execute(sql, (class_id,))
        except psycopg2.errors.ForeignKeyViolation as e:

            # print(e)

            return {

                'status': -1,
                'errorCode': 106112,
                'errorMsg': str(e),
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "class删除成功",                                     
            }

    async def update_class(self, class_id, class_name):#更新库房名称

        #参数检查
        if not all([class_id, class_name]) or not class_id.isdecimal():


            return {
                'status': -1,
                'errorCode': 106106,
                'errorMsg': '接口有两个必传参数且class_id为整数',
                'resultBody': None
            }

        sql = "UPDATE class SET class_name=%s WHERE id=%s"


        await ConnPG().execute(sql, (class_name,class_id))

    
        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "class修改成功",
        }

